﻿using CommonModel.Util;
using GXRTBTC.PublicClassLibrary.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace GXRTBTC.PublicClassLibrary.Model
{
    /**********************通用返回*******************************************************/
    /// <summary>
    /// 状态返回接口
    /// </summary>
    public interface IRes
    {
        public WebApiStatusCode Code { get; set; }
        public string Message { get; set; }
        //时间戳
        public string Timestamp { get; }
    }

    /// <summary>
    /// 通用无数据状态返回
    /// </summary>
    public class CommonRes : IRes
    {
        public WebApiStatusCode Code { get; set; } = WebApiStatusCode.code_200;
        public string Message { get; set; } = "success";
        //时间戳
        public string Timestamp
        {
            get
            {
                return TransForm.GetFormatDatetime();
            }
        }
    }

    /// <summary>
    /// 通用数据状态返回
    /// </summary>
    public class CommonDataRes : CommonRes
    {
        //消息数据
        public Object Data { get; set; }
    }
    /**********************通用返回*******************************************************/

    /**********************新WebApi接口*******************************************************/
    /// <summary>
    /// 消息接口
    /// </summary>
    public interface IWebApiData
    {
        //请求编号
        public string RequestId { get; set; }
        //命令
        public string Command { get; set; }
        //设备或系统通信协议版本
        public string Version { get; set; }
        //消息数据
        public Object Data { get; set; }
        //时间戳
        public string Timestamp
        {
            get
            {
                return TransForm.GetFormatDatetime();
            }
        }
    }

    /// <summary>
    /// 请求参数
    /// </summary>
    public class WebParamData
    {
        /// <summary>
        /// 参数是否正确
        /// </summary>
        public bool nowmal { get; set; } = false;
        /// <summary>
        /// 参数异常提示
        /// </summary>
        public string msg { get; set; } = "";

        //API 协议版本
        public string apiver { get; set; } = "";
        //级别 SYSTEM(系统级) DEVICE(设备级)
        public string level { get; set; } = "";
        //位置编号，xx_xxx_xxx，（对应模型中的位置编号）
        public string positionid { get; set; } = "";
        //系统/设备序列号，要求唯一性，不可更改
        public string sn { get; set; } = "";
    }

    /// <summary>
    /// 完整请求体，包含请求参数
    /// </summary>
    public class WebApiDataFull
    {
        /// <summary>
        /// 请求参数
        /// </summary>
        public WebParamData webParamData { get; set; } = new WebParamData();
        /// <summary>
        /// 是否WebSocket请求
        /// </summary>
        public bool IsWebSocket { get; set; } = true;
        /// <summary>
        /// 通用请求时，不检查参数
        /// 如：下行控制服务往采集服务发送下发指令
        /// </summary>
        public bool CheckParam { get; set; } = true;
        //请求内容
        public Dictionary<string, Object> dictData { get; set; }
        public IWebApiData ContentReq { get; set; } = new WebApiDataReqWS();
        public IWebApiData ContentRes { get; set; } = new WebApiDataResWS();

        /// <summary>
        /// 默认构造函数
        /// 反序列化时会用到
        /// </summary>
        public WebApiDataFull() : this(true) { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="IsWebSocket"></param>
        public WebApiDataFull(bool IsWebSocket)
        {
            this.IsWebSocket = IsWebSocket;
            if (!this.IsWebSocket)
            {
                ContentReq = new WebApiDataReq();
                ContentRes = new WebApiDataRes();
            }
        }
    }

    /// <summary>
    /// https请求消息
    /// </summary>
    public class WebApiDataReq : IWebApiData
    {
        public string RequestId { get; set; } = TransForm.DateTimeFormat(DateTime.Now, "yyyyMMddHHmmssfffffff");
        public string Command { get; set; } = "Default";
        public string Version { get; set; } = "V1.0.0";
        public Object Data { get; set; } = new();
        public string Timestamp
        {
            get
            {
                return TransForm.GetFormatDatetime();
            }
        }
    }

    /// <summary>
    /// https返回消息
    /// </summary>
    public class WebApiDataRes : WebApiDataReq
    {
        //状态码
        public WebApiStatusCode Code { get; set; } = WebApiStatusCode.code_200;
        //响应消息
        public string Message { get; set; } = EnumHelper.GetDescription(WebApiStatusCode.code_200);
    }

    /// <summary>
    /// websocket请求消息
    /// </summary>
    public class WebApiDataReqWS : WebApiDataReq
    {
        //消息类型
        public string Type { get; set; } = WebApiTypeCodeWS.unknown.ToString();
    }

    /// <summary>
    /// websocket返回消息
    /// </summary>
    public class WebApiDataResWS : WebApiDataRes
    {
        //消息类型
        public string Type { get; set; } = WebApiTypeCodeWS.unknown.ToString();
    }

    /// <summary>
    /// 监控系统WEB API接口协议
    /// 消息类型
    /// websocket专用
    /// </summary>
    public enum WebApiTypeCodeWS
    {
        [Description("未知")]
        unknown,
        [Description("认证消息")]
        auth,
        [Description("心跳消息")]
        heartbeat,
        [Description("数据消息")]
        data,
        [Description("错误消息")]
        error,
        [Description("关闭连接")]
        close
    }

    /// <summary>
    /// 监控系统WEB API接口协议
    /// 状态码
    /// </summary>
    public enum WebApiStatusCode
    {
        [Description("成功")]
        code_200 = 200,  //成功
        [Description("请求参数错误")]
        code_400 = 400,  //请求参数错误
        [Description("未授权")]
        code_401 = 401,  //未授权
        [Description("服务下线")]
        code_402 = 402,  //服务下线
        [Description("禁止访问，无权限操作")]
        code_403 = 403,  //禁止访问，无权限操作
        [Description("资源不存在")]
        code_404 = 404,  //资源不存在
        [Description("请求实体数据大小超过限制")]
        code_406 = 406,  //请求实体数据大小超过限制
        [Description("请求过期")]
        code_407 = 407,  //请求过期
        [Description("请求频率或数量超过限制")]
        code_411 = 411,  //请求频率或数量超过限制
        [Description("服务器内部错误")]
        code_500 = 500,  //服务器内部错误
        [Description("操作失败")]
        code_501 = 501,  //操作失败
        [Description("查询时间超出跨度")]
        code_4001 = 4001,    //查询时间超出跨度
        [Description("请求时间失效或过期")]
        code_5501 = 5501,    //The request has been expired！请求时间失效或过期
        [Description("服务异常失败")]
        code_5503 = 5503,    //Some Exception Failure！服务异常失败
        [Description("数据解析失败")]
        code_5504 = 5504,	//Decrypt failure！数据解析失败
    }
    /**********************新WebApi接口*******************************************************/
}
